using System;
using UnityEngine;

namespace MaterialUI
{
	public abstract class AutoTween
	{
		protected int m_TweenId = -1;

		protected bool m_Active;

		protected bool m_WaitingForDelay;

		protected Action m_Callback;

		protected float m_StartTime;

		protected float m_DeltaTime;

		protected float m_Duration;

		protected float m_Delay;

		protected bool m_ScaledTime;

		private AnimationCurve[] m_AnimationCurves;

		protected Tween.TweenType m_TweenType;

		protected AnimationCurve m_CustomCurve;

		public int tweenId => m_TweenId;

		protected abstract int ValueLength();

		protected abstract void OnUpdateValue();

		protected abstract void OnFinalUpdateValue();

		protected abstract float GetValue(bool isEnd, int valueIndex);

		public void Initialize(float duration, float delay, Tween.TweenType tweenType, Action callback, AnimationCurve animationCurve, bool scaledTime, int id)
		{
			m_Duration = duration;
			m_Delay = delay;
			m_TweenType = tweenType;
			m_Callback = callback;
			m_CustomCurve = animationCurve;
			m_ScaledTime = scaledTime;
			m_TweenId = id;
			if (m_Delay > 0f)
			{
				m_WaitingForDelay = true;
			}
			else
			{
				m_WaitingForDelay = false;
				StartTween();
			}
			m_Active = true;
		}

		protected virtual void StartTween()
		{
			SetupCurves();
			m_StartTime = ((!m_ScaledTime) ? Time.unscaledTime : Time.time);
		}

		public virtual void UpdateTween()
		{
			if (!m_Active)
			{
				return;
			}
			if (m_WaitingForDelay)
			{
				m_Delay -= ((!m_ScaledTime) ? Time.unscaledDeltaTime : Time.deltaTime);
				if (m_Delay <= 0f)
				{
					StartTween();
					m_WaitingForDelay = false;
				}
				return;
			}
			m_DeltaTime = ((!m_ScaledTime) ? (Time.unscaledTime - m_StartTime) : Time.time);
			if (m_DeltaTime < m_Duration)
			{
				try
				{
					OnUpdateValue();
				}
				catch (Exception)
				{
					EndTween(callback: false);
				}
				return;
			}
			try
			{
				OnFinalUpdateValue();
			}
			catch
			{
			}
			m_Active = false;
			EndTween(callback: true);
		}

		public void EndTween(bool callback)
		{
			if (callback)
			{
				m_Callback.InvokeIfNotNull();
			}
			m_TweenId = -1;
			TweenManager.Release(this);
		}

		private void SetupCurves()
		{
			m_AnimationCurves = new AnimationCurve[ValueLength()];
			Keyframe[][] array = new Keyframe[ValueLength()][];
			if (m_TweenType == Tween.TweenType.Custom)
			{
				for (int i = 0; i < ValueLength(); i++)
				{
					array[i] = m_CustomCurve.keys;
				}
			}
			else
			{
				float[][] animCurveKeys = Tween.GetAnimCurveKeys(m_TweenType);
				for (int j = 0; j < ValueLength(); j++)
				{
					array[j] = new Keyframe[animCurveKeys.Length];
					for (int k = 0; k < array[j].Length; k++)
					{
						array[j][k].time = animCurveKeys[k][0];
						array[j][k].value = animCurveKeys[k][1];
					}
				}
			}
			for (int l = 0; l < ValueLength(); l++)
			{
				for (int m = 0; m < array[l].Length; m++)
				{
					array[l][m].value *= GetValue(isEnd: true, l) - GetValue(isEnd: false, l);
					array[l][m].value += GetValue(isEnd: false, l);
					array[l][m].time *= m_Duration;
				}
				m_AnimationCurves[l] = new AnimationCurve(array[l]);
				if (m_CustomCurve == null)
				{
					for (int n = 0; n < array[l].Length; n++)
					{
						m_AnimationCurves[l].SmoothTangents(n, 0f);
					}
				}
			}
		}
	}
}
